import json  # 引入json表


# 1，读取文件
def read_data(path):  # 设置一个变量
    with open(path, mode='r', encoding='utf-8') as f:  # 打开文件以utf-8格式打开
        data = json.load(f)  # 读取文件
    return data


# 2，写入文件
def write_data(data_list, path):  # 设置一个函数变量
    with open(path, mode='w', encoding='utf-8', ) as f:  # 读取数据
        json.dump(data_list, f, ensure_ascii=False)  # 数据写入，不用转换


if __name__ == '__main__':
    # 测一下read_data
    path = r'users.json'  # 变量指定
    # print(read_data(path))   # 打印path值
    user_list = [{"username": "xiaozhang", "password": "123"}, {"username": "xiaoli", "password": "123"},
                 {"username": "xiaowang", "password": "123"}]
    write_data(user_list,path)



